// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {

  //获取信息
  const wxContext = cloud.getWXContext()
  
  //临时变量
  var headUrl = event.avatarUrl
  var nickname = event.nickName
  //数据库操作
  const db = cloud.database()
  const tb = db.collection('mine')
  //如果openid存在，不管，否则新增记录
  let id = wxContext.OPENID
  var flag = 0
  tb.where({
    openid : id
  }).get().then(res=>{
      if(res.data.length != 0)
        flag = 1
  })
  
  if(flag == 0){
    tb.add({
      data: {
        _openid: id,
        dealCount: 0,
        likes: 0,
        buyCar: [],

        minePull: {
          id: 0,
          title: "我的发布",
          num: "0"
        },

        mineBuyCar: {
          id: 1,
          title: "购物车",
          num: "0"
        }
      }
    })
  }

  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}